<template>
  <div>
    <div class="n-layout-page-header">
      <a-card :bordered="false" title="完整表单">
        useForm 表单用法，用于向用户收集表单信息，以下展示所有表单组件示例
      </a-card>
    </div>
    <a-card :bordered="false" class="mt-3 proCard">
      <div class="BasicForm">
        <BasicForm @register="register" @submit="handleSubmit" @reset="handleReset">
          <template #statusSlot="{ model, field }">
            <a-input v-model:value="model[field]" placeholder="请输入" />
          </template>
        </BasicForm>
      </div>
    </a-card>
  </div>
</template>

<script lang="ts" setup>
  import { ref } from 'vue';
  import { h } from 'vue';
  import { IconCalendar } from '@arco-design/web-vue/es/icon';
  import { BasicForm, useForm, FormSchema } from '@/components/Form/index';
  import { Message } from '@arco-design/web-vue';

  const autoCompleteData = ref<string[]>([]);

  const cascaderData = ref<any[]>([
    {
      value: 'beijing',
      label: 'Beijing',
      children: [
        {
          value: 'chaoyang',
          label: 'ChaoYang',
          children: [
            {
              value: 'datunli',
              label: 'Datunli',
            },
          ],
        },
        {
          value: 'haidian',
          label: 'Haidian',
        },
        {
          value: 'dongcheng',
          label: 'Dongcheng',
        },
        {
          value: 'xicheng',
          label: 'Xicheng',
          children: [
            {
              value: 'jinrongjie',
              label: 'Jinrongjie',
            },
            {
              value: 'tianqiao',
              label: 'Tianqiao',
            },
          ],
        },
      ],
    },
    {
      value: 'shanghai',
      label: 'Shanghai',
      children: [
        {
          value: 'huangpu',
          label: 'Huangpu',
        },
      ],
    },
  ]);

  const transferData = Array(8)
    .fill(undefined)
    .map((_, index) => ({
      value: `品牌${index + 1}`,
      label: `品牌 ${index + 1}`,
    }));

  const TreeSelectData = ref<any[]>([
    {
      key: '主治医生',
      icon: () => h(IconCalendar),
      title: '主治医生',
      children: [
        {
          key: 'node2',
          title: '小明',
        },
      ],
    },
    {
      key: '教授医生',
      title: '教授医生',
      icon: () => h(IconCalendar),
      children: [
        {
          key: 'node4',
          title: '张三',
        },
        {
          key: 'node5',
          title: '李四',
        },
      ],
    },
  ]);

  const schemas: FormSchema[] = [
    {
      field: 'name',
      component: 'Input',
      label: '姓名',
      labelMessage: '这是一个提示',
      componentProps: {
        placeholder: '请输入姓名',
        onChange: (e: any) => {
          console.log(e);
        },
      },
      rules: [{ required: true, message: '请输入姓名', trigger: ['blur'] }],
    },
    {
      field: 'mobile',
      component: 'InputNumber',
      label: '手机',
      componentProps: {
        placeholder: '请输入手机号码',
        hideButton: false,
        onChange: (e: any) => {
          console.log(e);
        },
      },
    },
    {
      field: 'type',
      component: 'Select',
      label: '类型',
      componentProps: {
        placeholder: '请选择类型',
        options: [
          {
            label: '舒适性',
            value: 1,
          },
          {
            label: '经济性',
            value: 2,
          },
        ],
        onChange: (e: any) => {
          console.log(e);
        },
      },
    },
    {
      field: 'status',
      label: '状态',
      //插槽
      slot: 'statusSlot',
    },
    {
      field: 'approver',
      label: '审批人',
      component: 'AutoComplete',
      componentProps: {
        placeholder: '请输入审批人，自动补全',
        data: autoCompleteData,
        onSearch: (value) => {
          if (value) {
            autoCompleteData.value = [...Array(5)].map((_, index) => `${value}-${index}`);
          } else {
            autoCompleteData.value = [];
          }
        },
      },
    },
    {
      field: 'address',
      label: '所在地址',
      component: 'Cascader',
      componentProps: {
        placeholder: '请选择',
        options: cascaderData,
        onChange: (value) => {
          console.log(value);
        },
      },
    },
    {
      field: 'detail',
      label: '详细地址',
      component: 'Textarea',
      componentProps: {
        placeholder: '请输入',
        onChange: (value) => {
          console.log(value);
        },
      },
    },
    {
      field: 'hobby',
      label: '爱好',
      component: 'InputTag',
      componentProps: {
        placeholder: '请输入，然后按回车键确认',
        onChange: (value) => {
          console.log(value);
        },
      },
    },
    {
      field: 'inform',
      label: '通知人',
      component: 'Mention',
      componentProps: {
        placeholder: '请输入@，然后选择通知人',
        data: ['小马哥', 'jack', 'roy', '啊俊'],
        onChange: (value) => {
          console.log(value);
        },
      },
    },
    {
      field: 'brand',
      label: '意向品牌',
      component: 'Transfer',
      componentProps: {
        placeholder: '请选择',
        data: transferData,
        showSearch: true,
        style: {
          width: '100%',
        },
        title: ['品牌列表', '意向品牌'],
        onChange: (value) => {
          console.log(value);
        },
      },
    },
    {
      field: 'attending',
      label: '主治医生',
      component: 'TreeSelect',
      componentProps: {
        placeholder: '请选择',
        data: TreeSelectData,
        onChange: (value) => {
          console.log(value);
        },
      },
    },
    {
      field: 'makeDate',
      component: 'DatePicker',
      label: '预约时间',
      //defaultValue: 1183135260000,
      componentProps: {
        onChange: (e: any) => {
          console.log(e);
        },
      },
    },
    {
      field: 'makeTime',
      component: 'TimePicker',
      label: '停留时间',
      componentProps: {
        allowClear: true,
        onChange: (e: any) => {
          console.log(e);
        },
      },
    },
    {
      field: 'first',
      label: '首次预约',
      component: 'Switch',
      componentProps: {
        placeholder: '请选择',
        onChange: (value) => {
          console.log(value);
        },
      },
    },
    {
      field: 'makeProject',
      component: 'Checkbox',
      label: '预约项目',
      componentProps: {
        placeholder: '请选择预约项目',
        options: [
          {
            label: '种牙',
            value: 1,
          },
          {
            label: '补牙',
            value: 2,
          },
          {
            label: '根管',
            value: 3,
          },
        ],
        onChange: (e: any) => {
          console.log(e);
        },
      },
    },
    {
      field: 'makeSource',
      component: 'RadioGroup',
      label: '来源',
      componentProps: {
        options: [
          {
            label: '网上',
            value: 1,
          },
          {
            label: '门店',
            value: 2,
          },
        ],
        onChange: (e: any) => {
          console.log(e);
        },
      },
    },
    {
      field: 'score',
      label: '评分',
      component: 'Rate',
      componentProps: {
        placeholder: '请选择',
        onChange: (value) => {
          console.log(value);
        },
      },
    },
    {
      field: 'score',
      label: '距离',
      component: 'Slider',
      componentProps: {
        placeholder: '请选择',
        defaultValue: 5,
        max: 15,
        marks: {
          0: '0km',
          5: '5km',
          10: '10km',
          15: '15km',
        },
        onChange: (value) => {
          console.log(value);
        },
      },
    },
  ];

  const [register, {}] = useForm({
    rowProps: { gutter: [16, 0] },
    actInheritLabelCol: true,
    submitButtonText: '提交预约',
    isFull: false,
    schemas,
  });

  function handleSubmit(values: Recordable) {
    console.log(values);
    Message.success(JSON.stringify(values));
  }

  function handleReset(values: Recordable) {
    console.log(values);
  }
</script>

<style lang="less" scoped>
  .BasicForm {
    width: 550px;
    margin: 0 auto;
    overflow: hidden;
    padding-top: 20px;
  }
</style>
